package com.mdlib.helper.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;

import com.ljlib.core.app.AppManager;
import com.ljlib.help.R;
import com.zhy.autolayout.AutoLayoutActivity;


/**
 * Created by bin on 16/10/21.
 */

public abstract class BaseActivity extends AutoLayoutActivity {

    private boolean animationAvailable = false;


    //获取Intent
    protected void handleIntent(Intent intent) {
    }

    //布局文件ID
    protected abstract int getContentViewId();

    //布局中Fragment的ID
    protected abstract int getFragmentContentId();


    //获取第一个fragment
    protected abstract BaseFragment getFirstFragment();


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppManager.getAppManager().addActivity(this);
        setContentView(getContentViewId());
        if (getIntent() != null) {
            handleIntent(getIntent());
        }

        //避免重复添加Fragment
        if (null == getSupportFragmentManager().getFragments()) {
            BaseFragment firstFragment = getFirstFragment();
            if (null != firstFragment) {
                addFragment(firstFragment);
                animationAvailable = true;
            } else {
                throw new NullPointerException("getFirstFragment() cannot be null");
            }
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppManager.getAppManager().removeActivity(this);
    }


    //添加fragment
    protected void addFragment(BaseFragment fragment) {
        if (fragment != null) {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
//            if (animationAvailable) {
//                transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
//            }
            transaction.replace(getFragmentContentId(), fragment, fragment.getClass().getSimpleName())
                    .addToBackStack(fragment.getClass().getSimpleName())
                    .commitAllowingStateLoss();
        }
    }

    //移除fragment
    protected void removeFragment() {
        if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
            getSupportFragmentManager().popBackStack();
        } else {
            finish();
        }
    }

    //返回键返回事件
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (KeyEvent.KEYCODE_BACK == keyCode) {
            if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
                finish();
                return true;
            } else {
                removeFragment();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
        if (fragment != null && fragment instanceof BaseFragment) {
            fragment.onActivityResult(requestCode, resultCode, data);
        }
    }
}
